#include "N1Log.h"
#include "N1Lib.h"

#ifdef WINDOWS
#include "shlobj.h"
#endif

N1UnicodeString* N1HomeDirectory()
{
#ifdef WINDOWS
    wchar_t path[MAX_PATH];
    if(SHGetSpecialFolderPathW(NULL, path, CSIDL_APPDATA, FALSE)==S_OK)
    {
        return (new N1UnicodeString(path))->autorelease<N1UnicodeString>();
    }
#else
#endif
    return NULL;
}

N1UnicodeString* N1CurrentDirectory()
{
#ifdef WINDOWS
    N1UnicodeString *ret = (new N1UnicodeString())->autorelease<N1UnicodeString>();
    DWORD size = 16;

    ret->setLength(size);

    while(1)
    {
        DWORD cnt = GetCurrentDirectoryW(size, ret->str());
        if(cnt==0)
        {
            N1LogError1("GetCurrentDirectoryW failed with %u" ,GetLastError());
        }
        if(cnt>=size)
        {
            size += 16;
            ret->setLength(size);
        } 
        else break;
    }
#else
    N1UnicodeString *ret = (new N1UnicodeString())->autorelease<N1UnicodeString>();

#endif
    return ret;
}


static FILE* gN1LogFile;

void N1LogInitialize()
{
    if (!gN1LogFile)
    {
        gN1LogFile = stderr;
    }
}

void N1Log(N1LogLevel logLevel, const char* Format, ...)
{
    va_list argList;
    va_start( argList, Format );

    vfprintf(gN1LogFile, Format, argList);
    time_t t = time(NULL);
    fprintf(gN1LogFile, " - %s\n", ctime( &t ) );

    va_end(argList);
}
